﻿using Common;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.SpecialPopulationAssessmentForm
{
    public partial class PrintSPAF : Form
    {
        #region 变量声明

        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        string Id = string.Empty;
        ElectronicSignature es;
        /// <summary>
        /// 多张打印 支持单张
        /// </summary>
        /// <param name="patientInfo">患者信息</param>
        /// <param name="id">若需要单张打印 请指定id 否则string.Empty </param>
        #endregion

        #region 构造函数
        public PrintSPAF(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.Id = id;
            es = new ElectronicSignature(this.LoginUser);
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\特殊人群评估单.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion


        public void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordOrderByTime(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.SpaForm, 1);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id.Equals(Convert.ToInt32(this.Id))).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    Spaf sf = XmlUtil.XMLToObject<Spaf>(record1.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string RYHS = sf.HuShiQianMing;
                    if (!string.IsNullOrEmpty(RYHS))
                    {
                        image1 = es.GetElectricSignBase64(record1.AreaId, sf.HuShiQianMing);
                    }
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,  //姓名
                        PatientInfo.SEX,  //性别
                        PatientInfo.AGE,  //年龄
                        PatientInfo.NURSE_CELL_NAME,   // 病区
                        PatientInfo.DEPARTMENT_NAME,   //科室名称
                        PatientInfo.BED_NO,  //床号
                        PatientInfo.NURSE_CELL_CODE,   //病案ID
                        sf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun,   //长期疼痛人群
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[0].Equals("1") ? 1 : 0,   //原因
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[1].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[2].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[3].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[4].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[5].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[6].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[7].Equals("1") ? 1 : 0,
                        sf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang,   //对患者影响
                        sf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao,  //一周内曾用药
                        sf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo,   //用药后效果
                        sf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing,  //患者对止痛药成瘾可能性
                        sf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi,   //处理措施_长期疼痛
                        sf.ZiShaQingXiangHuanZhe.ZiShaQingXiang,   //自杀（伤）倾向
                        sf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[0].Equals("1") ? 1 : 0,   //危险因素
                        sf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[1].Equals("1") ? 1 : 0,
                        sf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[2].Equals("1") ? 1 : 0,
                        sf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[3].Equals("1") ? 1 : 0,
                        sf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[4].Equals("1") ? 1 : 0,
                        sf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[5].Equals("1") ? 1 : 0,
                        sf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha,   //处理措施_自杀
                        sf.LinZhongHuanZhe.LZHZ,   //临终患者
                        sf.LinZhongHuanZhe.JiBingZhenDuan,   //疾病诊断
                        sf.LinZhongHuanZhe.ZhuYaoZhengZhuang,   //主要症状
                        sf.LinZhongHuanZhe.HuanZheQingXu,   //患者情绪
                        sf.LinZhongHuanZhe.HuanZheXuQiu,   //患者需求
                        sf.LinZhongHuanZhe.JiaRenXuQiu.Split('#')[0].Equals("1") ? 1 : 0,   //家人需求
                        sf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu,   //患者家属/临终抢救态度
                        sf.LinZhongHuanZhe.ChuLioCuo_LinZhong,   //处理措施_临终
                        sf.JinShenZhangAiHuanZhe.JingShenZhangAi,    //精神障碍
                        sf.JinShenZhangAiHuanZhe.JiaZuShi,   //家族史1
                        sf.JinShenZhangAiHuanZhe.ShouCiFaBingShiJian,    //家族史2--首次发病时间
                        sf.JinShenZhangAiHuanZhe.MoCiFaBingShiJian,   //家族史3--末次发病时间
                        sf.JinShenZhangAiHuanZhe.XinLiZhuangKuang,    //心理状况
                        sf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao,   //正规治疗1
                        sf.JinShenZhangAiHuanZhe.XiaoGuo,   //正规治疗2--效果
                        sf.JinShenZhangAiHuanZhe.FuFaXianXiang,   //复发现象
                        sf.JinShenZhangAiHuanZhe.PiHeZhiLiao,    //配合治疗
                        sf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen,   //处理措施_精神障碍
                        sf.XuJiuHuanZhe.XuJiu,  //酗酒
                        sf.XuJiuHuanZhe.XuJiuYunaYin,    //酗酒原因
                        sf.XuJiuHuanZhe.XuJiuQingKuang,  //酗酒情况
                        sf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing,   //酒精中毒性疾病
                        sf.XuJiuHuanZhe.XinLiYaLi_XuJiu,   //心理压力_酗酒
                        sf.XuJiuHuanZhe.ChuLioCuo_XuJiu,   //处理措施_酗酒
                        sf.XiDuHuanZhe.XDHZ,   //吸毒患者
                        sf.XiDuHuanZhe.XiDuFangShi,   //吸毒方式
                        sf.XiDuHuanZhe.XiDuPinLv,    //吸毒频率
                        sf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao,   //正规戒毒治疗 
                        sf.XiDuHuanZhe.FuXiXianXiang,    //复吸现象
                        sf.XiDuHuanZhe.XinLiYaLi_XiDu,    //心理压力_吸毒
                        sf.XiDuHuanZhe.KangHIV,   //抗HIV
                        sf.XiDuHuanZhe.HBsAg,  //HBsAg
                        sf.XiDuHuanZhe.ChuLioCuo_XiDu,    //处理措施_吸毒
                        !Base.ESSwitch.Equals("on") ? sf.HuShiQianMing : "",    //护士签名
                        sf.RiQi,     //日期
                        image1
                        );
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        #region 加载列头
        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));          //姓名
            dt.Columns.Add("SEX", typeof(string));                  //性别
            dt.Columns.Add("AGE", typeof(string));                  //年龄
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));     //病区
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));      //科室
            dt.Columns.Add("BED_NO", typeof(string));              //床号
            dt.Columns.Add("PATIENT_ID", typeof(string));         //病案号
            dt.Columns.Add("changqitengtongrenqun", typeof(string));    //长期疼痛人群
            dt.Columns.Add("yuanyin1", typeof(string));                 //原因1
            dt.Columns.Add("yuanyin2", typeof(string));                //原因2
            dt.Columns.Add("yuanyin3", typeof(string));                 //原因3
            dt.Columns.Add("yuanyin4", typeof(string));                //原因4
            dt.Columns.Add("yuanyin5", typeof(string));                 //原因5
            dt.Columns.Add("yuanyin6", typeof(string));                //原因6
            dt.Columns.Add("yuanyin7", typeof(string));                 //原因7
            dt.Columns.Add("yuanyin8", typeof(string));                //原因8
            dt.Columns.Add("duihuanzheyingxiang", typeof(string));     //对患者影响
            dt.Columns.Add("yizhouneicengyongyao", typeof(string));    //一周内曾用药
            dt.Columns.Add("yongyaohouxiaoguo", typeof(string));       //用药后效果
            dt.Columns.Add("huanzheDZTYCYKNX", typeof(string));       //患者对止痛药成瘾可能性
            dt.Columns.Add("chulicuoshichangqi", typeof(string));     //处理措施_长期疼痛
            dt.Columns.Add("zishaqingxiang", typeof(string));       //自杀（伤）倾向
            dt.Columns.Add("weixianyinsu1", typeof(string));        //危险因素1
            dt.Columns.Add("weixianyinsu2", typeof(string));        //危险因素2
            dt.Columns.Add("weixianyinsu3", typeof(string));        //危险因素3
            dt.Columns.Add("weixianyinsu4", typeof(string));        //危险因素4
            dt.Columns.Add("weixianyinsu5", typeof(string));        //危险因素5
            dt.Columns.Add("weixianyinsu6", typeof(string));        //危险因素6
            dt.Columns.Add("chulicuoshizisha", typeof(string));    //处理措施_自杀
            dt.Columns.Add("linzhonghuanzhe", typeof(string));        //临终患者
            dt.Columns.Add("jibingzhenduan", typeof(string));        //疾病诊断
            dt.Columns.Add("zhuyaozhengzhuang", typeof(string));     //主要症状
            dt.Columns.Add("huanzheqingxu", typeof(string));         //患者情绪
            dt.Columns.Add("huanzhexuqiu", typeof(string));          //患者需求
            dt.Columns.Add("jiarenxuqiu", typeof(string));           //家人需求
            dt.Columns.Add("huanzhejiashuLZQJTD", typeof(string));   //患者家属/临终抢救态度
            dt.Columns.Add("chulicuoshilinzhong", typeof(string));    //处理措施_临终
            dt.Columns.Add("jingshenzhangai", typeof(string));                //精神障碍
            dt.Columns.Add("jiazushi1", typeof(string));                      //家族史1
            dt.Columns.Add("jiazushi2", typeof(string));                      //家族史2--首次发病时间
            dt.Columns.Add("jiazushi3", typeof(string));                      //家族史3--末次发病时间
            dt.Columns.Add("xinlizhuangkuang", typeof(string));               //心理状况
            dt.Columns.Add("zhengguizhiliao1", typeof(string));               //正规治疗1
            dt.Columns.Add("zhengguizhiliao2", typeof(string));               //正规治疗2--效果
            dt.Columns.Add("fufaxianxiang", typeof(string));                  //复发现象
            dt.Columns.Add("peihezhiliao", typeof(string));                   //配合治疗
            dt.Columns.Add("chulicuoshijingshenzhangai", typeof(string));     //处理措施_精神障碍
            dt.Columns.Add("xujiu", typeof(string));             //酗酒
            dt.Columns.Add("xujiuyuanyin", typeof(string));      //酗酒原因
            dt.Columns.Add("xujiuqingkuang", typeof(string));    //酗酒情况
            dt.Columns.Add("jiujingzhongdu", typeof(string));    //酒精中毒性疾病
            dt.Columns.Add("xinluyali", typeof(string));         //心理压力_酗酒
            dt.Columns.Add("chulicuoshixujiu", typeof(string));  //处理措施_酗酒
            dt.Columns.Add("xiduhuanzhe", typeof(string));            //吸毒患者
            dt.Columns.Add("xidufangshi", typeof(string));            //吸毒方式
            dt.Columns.Add("xidupinlv", typeof(string));              //吸毒频率
            dt.Columns.Add("zhengguijieduzhiliao", typeof(string));   //正规戒毒治疗
            dt.Columns.Add("fuxixianziang", typeof(string));          //复吸现象
            dt.Columns.Add("xinli", typeof(string));                  //心理压力_吸毒
            dt.Columns.Add("kangHIV", typeof(string));               //抗HIV
            dt.Columns.Add("hbsag", typeof(string));                 //HBsAg
            dt.Columns.Add("chulicuoshixidu", typeof(string));       //处理措施_吸毒
            dt.Columns.Add("hushiqianming", typeof(string));        //护士签名
            dt.Columns.Add("riqi", typeof(string));                //日期
            dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
        }
        #endregion

        private void PrintSPAF_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }

        #region 检查数据项目
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        #endregion
    }
}
